package com.ecity.table 
{
	import com.ecity.events.GameEvent;
	import com.ecity.util.BitmapClip;
	import com.ecity.util.UIFactory;
	import flash.desktop.InteractiveIcon;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import gs.events.TweenEvent;
	import gs.TweenLite;
	/**
	 * ...
	 * @author ss
	 */
	public class Player extends TableDisplayObject 	{
		private var _index:int;
		private var _score:int;
		private var _ranking:int;
		
		private var _explode:BitmapClip; 
		
		private var _handL:Bitmap = new Bitmap();
		private var _handR:Bitmap = new Bitmap();
		
		
		private var _flickable:Boolean = true;
		
		private var _mindable:Boolean = false;
		private static const POSI:Array = [[500, 660, 0], [900, 380, -90], [500, 100, 180], [100, 380, 90]];
		private var confirm:Bitmap;
		private var wanted:Bitmap;
		//----------Class's user interface----------------
		private var _poker:Poker;
		//------------------------

		public function Player(index:int) {
			_index = index;
			this.x = POSI[index][0];
			this.y = POSI[index][1];
			this.rotation = POSI[index][2];
			_poker = new Poker();
			this.addChild(_poker);
			confirm = UIFactory.getBitmapbyName("b_okay");
			wanted = UIFactory.getBitmapbyName("b_wanted");
			_explode = new BitmapClip("explode");
			confirm.x  = 60;
			confirm.y  = -180;
			wanted.x  = -170;
			wanted.y  = -180;
			_poker.addEventListener(GameEvent.PUNISHED, onPunished);
		}
		
		public function showHand(isShowResult:Boolean = false):void {
			if (isShowResult) {
				_poker.showBack();
			}else{
				_poker.cover();
			}
		}
		
		
		
		public function reciveCard(cardIndex:int):void {
			_poker.cover();
			var value:int = cardIndex % 13 + 1;
			if (value > 10) { value = 10; }// all the j q k are considered as 10;
			if (_score > 21) {
				choose(false);
			}else {
				this._poker.showCard(cardIndex);
				_score += value;
			}
		}
		
		public function ationTosee():void {
			if(_flickable){
			TweenLite.to(_handL, 0.5, { alpha:1, onComplete:reHidehand } );
			TweenLite.to(_handR, 0.5, { alpha:1, onComplete:reHidehand } );
			}
		}
		private function reHidehand():void {
			TweenLite.to(_handL, 0.5, { alpha:0.2, onComplete:ationTosee} );
			TweenLite.to(_handR, 0.5, { alpha:0.2, onComplete:ationTosee} );
		}

		public function showChoseBtn(show:Boolean = true):void {
			_flickable = false;
			_handL.alpha = 0.2;	
			_handR.alpha = 0.2;	
			if (show) {
				this.addChild(confirm);
				this.addChild(wanted);
			}else {
				this.removeChild(confirm);
				this.removeChild(wanted);
			}
		}
		
		public function set ranking(value:int):void {
			this._ranking = value;
		}
		
		public function get ranking():int {
			if (_score == 21) {
				_ranking = 0;
			}
			return _ranking;
		}
		
		public function get score():int {
			if (_score == 21) {
				_score = 121;
			}
			return this._score;
		}
		
		public function set score(value:int):void {
			_score = value;
		}
		
		public function reset():void {
			_handL.alpha = 0;	
			_handR.alpha = 0;	
			score = 0;
			_poker.reStart();
			showScore(false);
		}
		
		private function selcetButtonOK():void {
			_mindable = true;
		}
		
		public function choose(isWanted:Boolean):void {
			if (isWanted) {
				this.dispatchEvent(new GameEvent(GameEvent.WANTED_CARD,{index:_index}));
			}else {
				showChoseBtn(false);
				this.dispatchEvent(new GameEvent(GameEvent.TOTHENEXT,{index:_index,score:this.score}));
			}
		}
		
		public function showRanking(ranking:int):void {
			this.ranking = ranking;
			//_poker.showRanking(ranking);
		}
		
		public function showScore(isShow:Boolean=true):void {
			var sl:Sprite = this.getLayer("scoreLayer");
			sl.y = -150;
			if (isShow) {
				_poker.uncover();
				var nn:ResourcesFont = new ResourcesFont();
				if (score == 121) {
					sl.addChild(_explode);
					_explode.x = -120;
					_explode.y = -150;
					_explode.gotoAndPlay(1, 21);
					var res:Bitmap = UIFactory.getBitmapbyName("maxValue");
					var mxv:Bitmap = new Bitmap(res.bitmapData.clone());
					mxv.x = -50;
					mxv.y = -30;
					sl.addChild(mxv);
				}else {
					sl.addChild(nn.getNumFont(score));
				}
				
			}else {
				while (sl.numChildren) {
					sl.removeChildAt(0);
				}
			}
		}
		
		private function onPunished(e:GameEvent):void {
			this.dispatchEvent(new GameEvent(GameEvent.PUNISHED, {index:_index,ranking:ranking}));
		}
		
	}

}